package com.coffeeloversupportlibrary.coffeelover.mercury.utils;

import com.coffeeloversupportlibrary.coffeelover.mercury.entity.CoffeeBeanSearchList;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Comment;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Game;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Register;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Shop;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.ShopRecord;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Type;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.User;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.WantOrDrinked;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 盛守义
 * @createTime 2021/10/25 11:19
 * @projectName TurningCoffee
 * @className JsonUtils.java
 * @description TODO
 */
public class JsonUtils {

    public String toJsonString(User user){
        Gson gson = new Gson();
        String jsonString = gson.toJson(user);
        return jsonString;
    }

    public Register getRegister(String res){
        Register register;
        try {
            JSONObject json = new JSONObject(res);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Gson gson = new Gson();
        register = gson.fromJson(res, Register.class);
        return register;
    }

    public User getUser(String res){
        Gson gson = new Gson();
        User user = gson.fromJson(res, User.class);
        return user;
    }

    public List<Type.DataDTO.TypeGroupDTO.SonTypesDTO> getSonList(String parentType, List<Type.DataDTO.TypeGroupDTO> typeGroups){
        List<Type.DataDTO.TypeGroupDTO.SonTypesDTO> list = new ArrayList<>();

        for(int i = 0; i < typeGroups.size(); i++){
            if(typeGroups.get(i).getParentType().getTypeName().equals(parentType)){
                list = typeGroups.get(i).getSonTypes();
            }
        }
        return list;
    }


    public CoffeeBeanSearchList getCoffeeSearchList(String res){
        Gson gson = new Gson();
        CoffeeBeanSearchList list = gson.fromJson(res, CoffeeBeanSearchList.class);
        return list;
    }

    public Shop getShop(String res){
        Gson gson = new Gson();
        Shop shops = gson.fromJson(res, Shop.class);
        return shops;
    }

    public Comment getComments(String res){
        Gson gson = new Gson();
        Comment comments = gson.fromJson(res, Comment.class);
        return comments;
    }

    public ShopRecord getShopRecard(String res){
        Gson gson = new Gson();
        ShopRecord shopRecord = gson.fromJson(res, ShopRecord.class);
        return shopRecord;
    }

    public WantOrDrinked getWantOrDrinked(String res){
        Gson gson = new Gson();
        WantOrDrinked wd = gson.fromJson(res, WantOrDrinked.class);
        return wd;
    }

    public Game getGame(String res){
        Gson gson = new Gson();
        Game game = gson.fromJson(res, Game.class);
        return game;
    }
}
